<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="字典名称">
        <el-input v-model="bindForm.name" placeholder="请输入字典名称" clearable />
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="bindForm.code" placeholder="请输入字典类型" clearable />
      </el-form-item>
      <el-form-item label="字典状态">
        <el-select v-model="bindForm.status" placeholder="字典状态" clearable>
          <el-option label="开启" :value="1" />
          <el-option label="关闭" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="openDialog">新建</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      style="width: 100%"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        prop="name"
        label="字典名称"
      />
      <el-table-column label="字典类型">
        <template slot-scope="scope">
          <!-- <router-link :to="'/dictItem/?id='+ scope.row._id">{{ scope.row.code }}</router-link> -->
          <el-link class="el-link--inner" @click="$router.push('/dictItem/?id='+ scope.row._id)">{{ scope.row.code }}</el-link>
        </template>
      </el-table-column>
      <!-- 用插槽方式把分类状态翻译成中文 -->
      <el-table-column
        prop="status"
        label="字典状态"
      >
        <template slot-scope="{row}">
          <span v-if="row.status===1">开启</span>
          <span v-if="row.status===0">关闭</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
      />
      <el-table-column label="创建时间">
        <template scope="scope">
          <span>{{ scope.row.createdAt|parseTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="primary"
            @click="changeStatus(scope)"
          >开启</el-button>
          <el-button
            v-else
            size="mini"
            @click="changeStatus(scope)"
          >关闭</el-button>
          <el-button
            size="mini"
            @click="findDictTypeFun(scope)"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope)"
          >删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 页数 -->
    <template>
      <div class="block">
        <el-pagination
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </template>
    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="字典类型" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sumibt('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getList } from '@/api/goods/goodsCategory.js'
import { getList, dictTypeCreate, editStatus, dictTypeDelete, findDictType, updateDictType } from '@/api/dictionaries/dictType.js'
import { parseTime } from '@/utils/index'
export default {
  filters: {
    parseTime(data) {
      return parseTime(data)
    }
  },
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        name: '', // 字典名称
        code: '', // 字典类型
        remark: '' // 备注
      },
      listLoading: true, // 表格的loading状态，初始值是true表示打开的时候。表格是在loading
      categoryId: '', // 根据是否有categoryId判断是弹窗新建还是修改
      title: '新建字典分类',
      pageNo: 1, // 第几页
      pageSize: 5, // 每页显示的条数
      total: 0, // 总共几条数据
      // 搜索输入框里的值
      bindForm: {
        name: null,
        code: null,
        status: null
      },
      searchForm: {
        name: null,
        code: null,
        status: null
      },
      rules: {
        name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入字典类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getListFun()
  },
  methods: {
    onSearch() {
      this.searchForm.name = this.bindForm.name
      this.searchForm.code = this.bindForm.code
      console.log(this.searchForm.code)
      this.searchForm.status = this.bindForm.status
      this.getListFun()
    },
    // 获取列表
    getListFun() {
      getList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        name: this.searchForm.name,
        code: this.searchForm.code,
        status: this.searchForm.status
      }).then(res => {
        console.log(res)
        this.listLoading = false
        const { code, data } = res
        if (code === 200) {
          this.total = data.pagination.count
          this.tableData = data.list
        }
      })
    },
    // 点击新增按钮，打开弹窗
    openDialog() {
      this.title = '新建字典类型'
      this.form = {
        name: '', // 字典名称
        code: '', // 字典类型
        remark: '' // 字典状态
      }
      this.dialogFormVisible = true
    },
    // 新建字典项
    dictTypeCreateFun() {
      dictTypeCreate(this.form).then(res => {
        const { code } = res
        if (code === 200) {
          this.dialogFormVisible = false
          this.getListFun()
        }
      })
    },
    // 查询
    findDictTypeFun({ row }) {
      this.openDialog()
      this.title = '修改字典分类'
      findDictType(row._id).then(res => {
        const { code, data } = res
        if (code === 200) {
          const { name, code, remark, _id } = data
          this.categoryId = _id
          this.form = {
            name,
            code,
            remark
          }
        }
      })
    },
    // 修改字典
    updateDictTypeFun() {
      updateDictType(this.categoryId, this.form).then(res => {
        // console.log(res)
        const { code } = res
        if (code === 200) {
          this.dialogFormVisible = false
          this.getListFun()
        }
      })
    },
    // 修改状态
    changeStatus({ row }) {
      const { status, _id } = row
      let _status = 0
      let message = '关闭成功'
      if (status === 0) {
        _status = 1
        message = '开启成功'
      } else {
        _status = 0
        message = '关闭成功'
      }
      editStatus(_id, { status: _status }).then(res => {
        // console.log("状态："res)
        const { code } = res
        if (code === 200) {
          this.$message({
            type: 'success',
            message
          })
          this.getListFun() // 请求成功刷新列表
        }
      })
    },
    sumibt(formName) {
      if (this.categoryId) {
        this.updateDictTypeFun()
      } else {
        this.dictTypeCreateFun()
      }
      // 表单验证
      this.$refs[formName].validate((valid) => {
        // if (valid) {
        //   alert('submit!')
        // } else {
        //   console.log('error submit!!')
        //   return false
        // }
      })
    },
    // 删除字典
    handleDelete({ row }) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dictTypeDelete(row._id).then(res => {
          const { code } = res
          if (code === 200) {
            this.getListFun()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getListFun()
      console.log(`每页${val}条`)
    },
    handleCurrentChange(val) {
      this.pageNo = val
      this.getListFun()
      console.log(`当前页：${val}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-link--inner{
  color:#3176fb
}
</style>
